package com.carrent.bus.service.impl;

import com.carrent.bus.domain.Car;
import com.carrent.bus.domain.Rent;
import com.carrent.bus.mapper.CarMapper;
import com.carrent.bus.mapper.RentMapper;
import com.carrent.bus.service.RentService;
import com.carrent.bus.vo.RentVo;
import com.carrent.sys.constast.SysConstast;
import com.carrent.sys.utils.DataGridView;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 */
@Service
public class RentServiceImpl implements RentService {
    @Autowired
    private RentMapper rentMapper;
    @Autowired
    private CarMapper carMapper;

    //保存出租单信息
    @Override
    @Transactional
    public int insertRentByRentVo(RentVo rentVo) {
        int i = rentMapper.insertSelective(rentVo);
        //修改车辆的是否出租的状态(0未租，1已租）
        Car car = new Car();
        car.setCarnumber(rentVo.getCarnumber());
        car.setIsrenting(SysConstast.RENT_CAR_TRUE);
        carMapper.updateByPrimaryKeySelective(car);
        return i;
    }

    //查询所有出租单
    @Override
    public DataGridView queryAllRent(RentVo rentVo) {
        Page<Object> page = PageHelper.startPage(rentVo.getPage(),rentVo.getLimit());
        List<Rent> data = rentMapper.queryAllRent(rentVo);
        return new DataGridView(page.getTotal(),data);
    }

    @Override
    @Transactional
    public void updateRent(RentVo rentVo) {
        rentMapper.updateByPrimaryKeySelective(rentVo);
    }

    @Override
    @Transactional
    public void deleteRent(RentVo rentVo) {
        String rentid = rentVo.getRentid();
        rentMapper.deleteByPrimaryKey(rentid);
    }
    //通过出租单号查询
    @Override
    public Rent queryRentByRentid(String rentId) {
        return rentMapper.queryRentByRentid(rentId);
    }
}
